package org.paradise.dms.web.action.swipcard;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.Swipcard;
import org.paradise.dms.services.SwipcardService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description:
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @Version1.0 2010-03-09 李韦 created
 */
@Service
@RemoteProxy(name = "swipcardAction")
public class SwipcardAction extends DMSBaseAction {

	private static Logger log = Logger.getLogger(SwipcardAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -7859390654711841354L;

	@Autowired
	@Qualifier("swipcardServiceImpl")
	private SwipcardService swipcardservice;
	private Swipcard sc;
	private List<Swipcard> sclist = null;
	private int id;// 刷卡记录ID
	private String cardno;
	private String studentno;
	private String studentname;
	private Timestamp swiptime;
    private String hostname;
    
    	public String getHostname() {
		return hostname;
	}
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCardno() {
		return cardno;
	}
	public void setCardno(String cardno) {
		this.cardno = cardno;
	}
	public String getStudentno() {
		return studentno;
	}
	public void setStudentno(String studentno) {
		this.studentno = studentno;
	}
	public String getStudentname() {
		return studentname;
	}
	public void setStudentname(String studentname) {
		this.studentname = studentname;
	}
	public Timestamp getSwiptime() {
		return swiptime;
	}
	public void setSwiptime(Timestamp swiptime) {
		this.swiptime = swiptime;
	}
	public SwipcardService getSwipcardservice() {
		return swipcardservice;
	}
	public void setSwipcardservice(SwipcardService swipcardservice) {
		this.swipcardservice = swipcardservice;
	}
	public Swipcard getSc() {
		return sc;
	}
	public void setSc(Swipcard sc) {
		this.sc = sc;
	}
	public List<Swipcard> getSclist() {
		return sclist;
	}
	public void setSclist(List<Swipcard> sclist) {
		this.sclist = sclist;
	}
	/**
	 * 
	 * Description: 增加一条刷卡记录信息
	 * 
	 * @Version1.0  2010-03-09 李韦 created
	 * @param swipcard
	 * @throws DaoException
	 */
	public String insertSwipcard() throws DaoException  {
		try {
			InetAddress netAddress;
			netAddress = InetAddress.getLocalHost();
			sc.setHostname(netAddress.getHostName());

		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		Date date = new Date(); 
		Timestamp nousedate = new Timestamp(date.getTime()); 
		sc.setSwiptime(nousedate);
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		String recorderName=(String)session.getAttribute("systemusername");
		sc.setSwipusername(recorderName);
		
		if (this.swipcardservice.insertSwipcard(this.sc)) {
			return getAllSwipcard();
		} else
			return INPUT;
	}
	/**
	 * 
	 * Description: 根据id取得一条刷卡记录
	 * 
	 * @Version1.0 2010-03-09 李韦 created
	 * @return
	 */
	public String findSCByID() throws DaoException {
		int swipcardid = Integer.parseInt(ServletActionContext.getRequest().getParameter("swipcardid"));
		List<Swipcard> sclist = swipcardservice.getSwipcard(swipcardid);
		if (sclist != null && sclist.size() > 0) {
			sc = sclist.get(0);
		}
		return SUCCESS;
	}

	/**
	 * 
	 * Description:获取所有的刷卡列表记录
	 * 
	 * @Version1.0 2010-03-09 李韦 created
	 * @return
	 * @throws DaoException
	 */
	public String getAllSwipcard() throws DaoException {
		
		try {
			InetAddress netAddress;
			netAddress = InetAddress.getLocalHost();
		    hostname = netAddress.getHostName();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		sclist = swipcardservice.getSwipcards(hostname);
		if (sclist != null && sclist.size() > 0) {
			return SUCCESS;
		}
		return INPUT;
	}

	/**
	 * 
	 * Description: 根据swipcardid删除一条记录
	 * 
	 * @Version1.0 2010-03-09 李韦 created
	 * @return
	 */
	public String swipcarddelete() {
		int swipcardid = Integer.parseInt(ServletActionContext.getRequest().getParameter("swipcardid"));
		if (swipcardservice.deleteSwipcard(swipcardid))
			return SUCCESS;
		else
			return INPUT;
	}

	
}
